package com.dal.module.game_server;

import org.bean.GameServerInfo;
import org.error.LoginErrorCode;

import com.dal.cache.GameServerInfoCache;
import com.dal.module.base.BaseController;
import com.jfinal.json.JsonManager;
import com.sojoys.artifact.annotation.IController;
import com.sojoys.artifact.tools.ToolError;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * Created by fxf on 2017/6/29.
 */
@IController("/gameServer")
public class GameServerController extends BaseController {

    static Log log = LogFactory.get(GameServerController.class);

    private static GameServerController instance = new GameServerController();

    public static GameServerController me(){
        return instance;
    }

//    public void createGameServer() {
//        String serverIp = getPara(ParamKey.SERVER_IP);
//        int serverPort = getParaToInt(ParamKey.SERVER_PORT, 0);
//        String serverName = getPara(ParamKey.SERVER_NAME);
//        int serverState = getParaToInt(ParamKey.SERVER_STATE, GameServerStateEnum.CLOSE.stateCode);
//        String dateStr = getPara(ParamKey.SERVER_OPEN_DATE);
//
//        Date openDate = DateTime.of(dateStr, "yyyy-MM-dd HH:mm:ss");
//
//        ToolError.isAndTrue(LoginErrorCode.MISS_PARAM_IP,"缺少ip",StringUtils.isEmpty(serverIp));
//        ToolError.isAndTrue(LoginErrorCode.MISS_PARAM_PORT,"缺少port",serverPort == 0);
//        ToolError.isAndTrue(LoginErrorCode.MISS_PARAM_ID,"缺少id",serverPort == 0);
//
//        GameServer gs = GameServer.builder();
//        gs.setServerIp(serverIp);
//        gs.setPort(serverPort);
//        gs.setServerName(serverName);
//        gs.setState(serverState);
//        gs.setOpenServerTime(openDate);
//
//        GameServerCache.me().insert(gs);
//
//        renderSuccess(gs, "");
//    }

//    public void changeGameServerState() {
//        int gameServerId = getParaToInt(ParamKey.SERVER_ID);
//        int state = getParaToInt(ParamKey.SERVER_STATE);
//        
//        ToolError.isAndTrue(LoginErrorCode.STATE_CODE_IS_ERR,"状态码不正确",ServerStatusEnum.RUNNING);
//        GameServer gs = GameServerCache.me().getById(gameServerId);
//        ToolError.isAndTrue(LoginErrorCode.GAME_SERVER_ID_IS_ERR,"状态码不正确",gs == null);
//
//        gs.setState(state);
//
//        renderSuccess(gs, "");
//    }


    /**
     * 获取推荐服务器
     */
//    public GameServer getRecommendServer() {
//        List<GameServer> list = GameServerCache.me().loadAll();
//        GameServer result = list.get(0);
//        for (GameServer gameServer : list) {
//            int state = gameServer.getState();
//            if (state == GameServerStateEnum.CLOSE.stateCode || state == GameServerStateEnum.HIDE.stateCode) {
//                continue;
//            }
//            if (state == GameServerStateEnum.GOOD.stateCode) {
//                result = gameServer;
//                break;
//            }
//            result = gameServer;
//        }
//        if(result == null){
//            log.error("新建账号 --> 没有获取到游戏服");
//            ToolError.isAndTrue(LoginErrorCode.CAN_NOT_FIND_GAME_SERVER,"没有获取到游戏服",result == null);
//        }
//        return result;
//    }
    
    
    /**
     * 获取推荐服务器
     */
    public GameServerInfo getRecommendServer() {
        GameServerInfo gameServerInfo = GameServerInfoCache.getOptimal();
        if(gameServerInfo == null){
            log.error("新建账号 --> 没有获取到游戏服");
            ToolError.isAndTrue(LoginErrorCode.CAN_NOT_FIND_GAME_SERVER,"没有获取到游戏服",gameServerInfo == null);
        }
        return gameServerInfo;
    }
    
    
    public void servers() {
    	renderSuccess(GameServerInfoCache.getServers(), "");
    }
}
